package com.example.module.iot.controller;

import com.example.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.example.framework.common.pojo.R;
import com.example.module.iot.aws.util.AwsMqttUtils;
import com.example.module.iot.emq.util.EmqxUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Tag(name = "iot")
@RestController
@RequestMapping("/iot")
public class IotController {

    @Autowired(required = false)
    private AwsMqttUtils awsMqttUtils;

    @Operation(summary = "发布emqx mqtt消息")
    @GetMapping("/emqx/publish")
    public R<?> publishEmqxMessage(@RequestParam String topic, @RequestParam String message) {
        EmqxUtils.publish(topic, message);
        return R.success();
    }

    @Operation(summary = "发布aws mqtt消息")
    @GetMapping("/aws/publish")
    public R<Integer> publishAwsMessage(@RequestParam String topic, @RequestParam String message) {
        if (Objects.isNull(awsMqttUtils)) {
            return R.fail(GlobalErrorCodeConstants.NOT_IMPLEMENTED.getCode(), "aws mqtt 未启用");
        }
        Integer messageId = awsMqttUtils.publish(topic, message);
        return R.success(messageId);
    }

}
